package com.javaclimb.music.service.impl;

import com.javaclimb.music.dao.RankMapper;
import com.javaclimb.music.domain.Rank;
import com.javaclimb.music.service.RankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 【评价service实现
 *
 * */
@Service
public class RankServiceImpl implements RankService {

    @Autowired
    private RankMapper rankMapper;

    /**
     * 增加
     *
     * @param rank
     */
    @Override
    public boolean insert(Rank rank) {
        return rankMapper.insert(rank) > 0;
    }

    /**
     * 查总评分人数
     *
     * @param songListId
     */
    @Override
    public int selectRankNum(Long songListId) {
        return rankMapper.selectRankNum(songListId);
    }

    /**
     * 查总评分
     *
     * @param songListId
     */
    @Override
    public int selectScoreSum(Long songListId) {
        return rankMapper.selectScoreSum(songListId);
    }

    /**
     * 计算平均分
     *
     * @param songListId
     */
    @Override
    public int rankOfSongListId(Long songListId) {
        int rankNum = rankMapper.selectRankNum(songListId);
        if(rankNum == 0){
            return 5;
        }
        return rankMapper.selectScoreSum(songListId) / rankNum;
    }

    /**
     * 是否已评价
     *
     * @param rank
     */
    @Override
    public int isRank(Rank rank) {
        return rankMapper.isRank(rank) != null ? (Integer)rankMapper.isRank(rank) : -1;
    }
}
